package com.cn.api.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.cn.api.dao.UserMapper;
import com.cn.api.error.ErrorCode;
import com.cn.api.error.ExceptionExpand;
import com.cn.api.service.UserService;
import com.cn.api.util.Const;
import com.cn.api.util.HttpRequest;
import com.cn.api.util.StringUtil;
import com.cn.api.vi.User;
import com.github.pagehelper.PageHelper;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.List;

@Service("userServiceImpl")
public class UserServiceImpl implements UserService {
    @Resource
    UserMapper userMapper;
    @Resource
    RedisTemplate<String, Object> redisTemplate;

    @Override
    public void addUser(String name, String password, User user) throws ExceptionExpand {
        if (user == null) {
            throw new ExceptionExpand(ErrorCode.CODE_NOTLOGIN_ERROR, ErrorCode.MESSAGE_NOTLOGIN_ERROR);
        }
        if (!Const.USERNAME.equalsIgnoreCase(user.getName())) {
            throw new ExceptionExpand(ErrorCode.CODE_ADMIN_ERROR, ErrorCode.MESSAGE_ADMIN_ERROR);
        }
        User user2 = userMapper.selectByName(name);
        if (user2 != null) {
            throw new ExceptionExpand(ErrorCode.CODE_USERNAME_EXIST_ERROR, ErrorCode.MESSAGE_USERNAME_EXIST_ERROR);
        }
        com.cn.api.model.User user1 = new com.cn.api.model.User();
        user1.setName(name);
        Date now = new Date();
        user1.setCreateTime(now);
        user1.setDelFlag(Const.DEL_FLAG_ON);
        user1.setUpdateTime(now);
        try {
            user1.setPassword(StringUtil.md5(password));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        userMapper.insertSelective(user1);
    }

    @Override
    public void deleteUser(Integer id, User user) throws ExceptionExpand {
        User user1 = userMapper.selectByPrimaryKey(id);
        if (user1 == null) {
            return;
        }
        if (user == null) {
            throw new ExceptionExpand(ErrorCode.CODE_NOTLOGIN_ERROR, ErrorCode.MESSAGE_NOTLOGIN_ERROR);
        }
        if ("admin".equals(user.getName())) {
            userMapper.deleteByPrimaryKey(id);
        } else {
            throw new ExceptionExpand(ErrorCode.CODE_ADMINPOWER_ERROR, ErrorCode.MESSAGE_ADMINPOWER_ERROR);
        }
    }

    @Override
    public User loginUser(String name, String password) throws ExceptionExpand {
        User user = userMapper.selectByName(name);
        if (user == null) {
            throw new ExceptionExpand(ErrorCode.CODE_LOGIN_ERROR, ErrorCode.MESSAGE_LOGIN_ERROR);
        }
        try {
            if (!user.getPassword().equalsIgnoreCase(StringUtil.md5(password))) {
                throw new ExceptionExpand(ErrorCode.CODE_PASSWORD_ERROR, ErrorCode.MESSAGE_PASSWORD_ERROR);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public List<com.cn.api.model.User> selectAllUser(int pageNum, int pageSize, String name) {
        PageHelper.startPage(pageNum, pageSize);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
//        redisTemplate.opsForList().range("users", 0, 1);
        List<com.cn.api.model.User> users = (List<com.cn.api.model.User>) redisTemplate.opsForList().leftPop("users");
//        List<com.cn.api.model.User> users = (List<com.cn.api.model.User>) redisTemplate.seth
        if (users == null) {
            System.out.println("查询数据库");
            users = userMapper.selectAll(name);
            redisTemplate.opsForValue().set("users", users);
        }
        return users;
    }

    @Override
    public void modifyPassword(User user, String oldPassword, String newPassword) throws ExceptionExpand {
        if (user == null) {
            throw new ExceptionExpand(ErrorCode.CODE_NOTLOGIN_ERROR, ErrorCode.MESSAGE_NOTLOGIN_ERROR);
        }
        try {
            if (!user.getPassword().equalsIgnoreCase(StringUtil.md5(oldPassword))) {
                throw new ExceptionExpand(ErrorCode.CODE_PASSWORD_ERROR, ErrorCode.MESSAGE_PASSWORD_ERROR);
            }
            user.setPassword(StringUtil.md5(newPassword));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        userMapper.updateByPrimaryKey(user);
    }

    @Override
    public void defaultPassword(Integer id, User user) throws ExceptionExpand {
        if (user == null) {
            throw new ExceptionExpand(ErrorCode.CODE_NOTLOGIN_ERROR, ErrorCode.MESSAGE_NOTLOGIN_ERROR);
        }
        if (!Const.USERNAME.equalsIgnoreCase(user.getName())) {
            throw new ExceptionExpand(ErrorCode.CODE_ADMIN_ERROR, ErrorCode.MESSAGE_ADMIN_ERROR);
        }
        User user1 = userMapper.selectByPrimaryKey(id);
        if (user1 == null) {
            throw new ExceptionExpand(ErrorCode.CODE_NOUSER_ERROR, ErrorCode.MESSAGE_NOUSERT_ERROR);
        }
        try {
            user1.setPassword(StringUtil.md5("111111"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        userMapper.updateByPrimaryKey(user1);
    }

    @Override
    public void post1(String prizeRecordID, String phoneInfo, String code, String cookie) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("prizeRecordID", prizeRecordID);
        jsonObject.put("phoneInfo", phoneInfo);
        jsonObject.put("code", code);
        jsonObject.put("activeCode", 1080);
        jsonObject.put("activePrizeCode", 3);
        HttpRequest.sendPostAddCookie("http://m.client.10010.com/myPrizeForActivity/activationOther/activationJDECard.htm", jsonObject.toJSONString(), cookie);
    }

    @Override
    public void sendCode(String phoneNum, String cookie) {
        HttpRequest.sendPostAddCookie("http://m.client.10010.com/myPrizeForActivity/activationOther/getcode.htm?v=" + phoneNum, "", cookie);
    }

}
